import httpclient from 'httpclient';
import api from '../../../../api/api_kind.js';
export default {
    name : "permission",
    data: function () {
        return {
            table: {
                api: api.BASE.ROLE.ROLE_LIST_PAGE,//角色列表api
                header:[
                    {"prop": "name", "label": "角色名称",isOpen:false},
                ],
            },
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            tree:{
                data:[],//权限列表
                visible:false,
                checks:[]//默认选中
            },
            role:{
                uid:""//角色ID
            }
        };
    },
    mounted:function(){
        this.init();//初始化权限数据结构
    },
    methods: {
        //初始化权限结构
        init(){
            const that = this;
            if(that.$store.state.menuStore.menu.length === 0){
                that.tree.visible = true;
                let tree = [];
                let treeNavs = [];
                let treeNavChild = [];
                httpclient.sendPost(api.BASE.MENU.MENU_LIST).done((res) => {
                    if (res.code === 1) {
                        that.tree.data=[];
                        res.data.forEach(function(treeParent){
                            if(treeParent.parent === null){
                                res.data.forEach(function(treeNav) {
                                    if(treeNav.parent === treeParent.id){
                                        res.data.forEach(function(treeChild) {
                                            if(treeChild.parent === treeNav.id){
                                                treeNavChild.push({id:treeChild.id,label:treeChild.name});
                                            }
                                        });
                                        treeNavs.push({id:treeNav.id,label:treeNav.name,children: treeNavChild});
                                        treeNavChild = [];
                                    }
                                });
                                tree.push({id:treeParent.id,label:treeParent.name,children: treeNavs});
                                treeNavs = [];
                                that.tree.visible = false;
                            }
                        });
                        that.tree.data = tree;
                        that.$store.commit("setMenu",tree);//提交权限到store
                    } else {
                        httpclient.errorPage("error", "获取数据失败", true);
                    }
                }).fail((error) => {
                    httpclient.errorPage("error", error.errorMsg, true);
                });
            }else{
                that.tree.data = this.$store.state.menuStore.menu;
            }
        },
        onHandleCurrentChange(val) {
            const that = this;
            if(null !== val){
                that.role.uid = val.id;
                httpclient.sendPost(api.BASE.MENU.MENU_LIST_BY_ROLE_ID, {roleId:that.role.uid}).done((res) => {
                    this.tree.visible = false;
                    if (res.code === 1) {
                        if(res.data !== null){
                            that.tree.checks = [];
                            res.data.forEach(function(tree){
                                if(tree.parent !== null){
                                    that.tree.checks.push({id:tree.id,label:tree.name});
                                }
                            });
                            that.$refs.tree.setCheckedNodes(that.tree.checks);
                        }
                    } else {
                        httpclient.errorPage("error", "获取数据失败", true);
                    }
                }).fail((error) => {
                    this.tree.visible = false;
                    httpclient.errorPage("error", error.errorMsg, true);
                });
            }
        },

        saveData(){
            const that = this;
            if(this.role.uid ===""){
                httpclient.errorPage("error", "请选择角色", true);
                return;
            }
            this.tree.visible = true;
            let trees = this.$refs.tree.getCheckedNodes().map(item => item.id).join();
            httpclient.sendPost(api.BASE.MENU.ADD_MENU, {roleId:this.role.uid,menuIds:trees}).done((res) => {
                that.tree.visible = false;
                if (res.code === 1) {
                    httpclient.errorPage("success", "分配权限成功", true);
                } else {
                    httpclient.errorPage("error", "获取数据失败", true);
                }
            }).fail((error) => {
                this.tree.visible = false;
                httpclient.errorPage("error", error.errorMsg, true);
            });
        }
    }
};
